# -*- coding: utf-8 -*-
import json
import re

def getALLSlotNamesBot(bot_file): #only grabbing names since we are not checking AMAZON.slot's values
    all_slots = []
    for intent in bot_file['bot']['intents']:
        if intent['slots']:
            for names in intent['slots']:
                all_slots.append(names['name'])
    return all_slots

def getBuiltInSlotNamesBot(bot_file):
    builtin_slots = []
    builtin_dict = {}
    for intent in bot_file['bot']['intents']:
        if intent['slots']:
            for slot in intent['slots']:
                if 'AMAZON' in slot['slotType']:
                    ## 这里需要去重判断一下
                    if slot['name'] not in builtin_slots:
                        builtin_slots.append(slot['name'])
                        builtin_dict[(slot['name'])] = slot['slotType']

    ## 这里只要返回一个就行，builtin_dict.keys()就等于
    return builtin_dict

def findSlots(column):  #will return a list of all slots in 3rd party adjudication file
    slot_dictionary = {}
    slot_values = {}
    slot_names =  {}
    for index, utterance in column.items():
        if type(utterance) == str:
            #pull all the information inside the brackets
            full_annotation = re.findall(r'\{[^\{\}]*\}',utterance) #keeps inner brackets (if nested)
            annotations_list = re.findall(r'\{([^\{\}]*)\}',utterance) #remove nested/unbalanced brackets for splitting value, name
            if annotations_list:
                slot_values[index] = [annotation.split('|')[0] for annotation in annotations_list]
                slot_names[index] = [annotation.split('|')[1]for annotation in annotations_list]
                slot_dictionary[index] = [annotation for annotation in annotations_list]
    return slot_dictionary, slot_values, slot_names


def getCustomSlotNameValuesBot(bot_file):
    '''gets the custom slotName and the slotValues from the application file'''
    translated_custom_slot_names = []
    intents = bot_file['bot']['intents']
    for intent in intents:
        slots = intent["slots"]
        for slot in slots:
            slottype = slot['slotType']
            name = slot['name']
            translated_custom_slot_names.append([slottype, name])

    custom_slots = []  #(slotValue, slotName) pairings
    custom_name =[]
    custom_values = []
    slot_types = bot_file['bot']['slotTypes']
    for slot in slot_types:
        slot_type_name = (slot['name'])  #name of the slot type
        #print(slot_name)
        #custom_name.append(slot_name)
        for translated_name in translated_custom_slot_names:
            for values in (slot['enumerationValues']):
                if slot_type_name == translated_name[0]:  #[0] represents the slottypeName
                    #print(slot_name,translated_name[0] )
                    custom_name.append(translated_name[1])  #if they share the same slotType, append the Name of the slot
                    slot_value = (values['value'])
                    custom_values.append(slot_value)
                    custom_slots.append((slot_value,translated_name[1]))

    return custom_slots, custom_values, custom_name

def load_botjson_info(bot_file_path):
    '''加载bot json相关信息'''
    with open(bot_file_path,'r',encoding='utf-8') as fread:
        bot_json_obj = json.load(fread)
        builtin_dict = getBuiltInSlotNamesBot(bot_json_obj)

        custom_slots, custom_values, custom_names = getCustomSlotNameValuesBot(bot_json_obj)
        custom_slot_dict = {value:name for value,name in zip(custom_values,custom_names)}
        return custom_slot_dict,builtin_dict